<?php
namespace app\common\controller;
define('NOW_TIME',time());

//短信接口
class Sms{

	//短信重发间隔时间
	private $sTime=60;
	
	//发送短信
	public function SmsSend($param){
		//手机号码
		$mobile = $param['mobile'];
		if(!$mobile || !is_mobile($mobile))return false;	
		
		//短信类型   1-用户注册，2-忘记密码  , 3-更换账号
		$type = $param['type'];
		$id_rtn=array(1,2,3);
		if(!in_array($type, $id_rtn))return false;				
					
	    //生成短信验证码
		$develop_mode=config('common.DEVELOP_MODE');
		$content = (1==$develop_mode)?8888:rand(1000, 9999);
			
		$rtn['sTime']=$this->sTime;
		$rtn['code']=0;
	
		//将短信内容记录到数据库中			
		$result = $this->saveSmsInfo($mobile, $content, $type);

		//写入失败操作				
		if(true!==$result){
				switch ($result){
					case 1:$rtn['msg']='短信已发送，请稍后再试';break;
					case 2:$rtn['msg']='记录添加失败';break;
				}
				return $rtn;
		}
		
		
		//发送短信-操作
		//TODO 调试短信发送不进行调用
        $type=(3==$type)?1:$type;
		$res = $this->_send($mobile, $content, $type);
		if(!$res){
			$rtn['msg']='短信发送失败';
			return $rtn;
		}
		
		$rtn['code']=1;
		$rtn['msg']='短信发送成功';
		return $rtn;		
	}	

	
	
	/**
	 * 发送短信接口
	 * @param string $mobile        手机号
	 * @param content $content      内容
	 * @param number $type          类型     1-注册  2-找回密码
	 */
	private function _send($mobile = '', $content = '', $type = 1) {
		$develop_mode=config('common.DEVELOP_MODE');
		if(1==$develop_mode)return true;
		//调用短信接口发送验证码
        require '../extend/lib/Smsali.php';
		 
		$entinfo_object = new \Smsali();
		$result = $entinfo_object->sendgxmsg($mobile,$type,$content);
		if($result==1)return true;		
		return false;		 
	}	
	
	
	
	//写入数据库验证手机号码
	//*+------------------------------------------------------------------------+*/	
	
			
    /**
     * 保存短信信息
     * @param string $mobile        手机号
     * @param int $type             类型      1-注册短信  2-找回密码
     * @param string $content       内容
     */
    public function saveSmsInfo($mobile, $content, $type) { 
    	//验证码有效时间，单位秒
    	$sTime=$this->sTime;    	       	       
        $object=db('sms_code');
        
        //验证短信码是否存在，是否过期
        $map['phone'] =  $mobile;
        $map['type'] = $type;       
        $result = $object->where($map)->find();        
                
        //判断验证码是否已经发送，还未过期
        if($result && $result['time']>(NOW_TIME-$sTime))return 1;
                
        $data = array(
            'phone' => $mobile,
            'content' => $content,
            'type' => $type,   
        	'time'=>NOW_TIME,         
        );
                
        //更新
        if($result){
        	$map['id']=$result['id'];       
            $object->removeOption()->where($map)->update($data);
        } else {        //添加
            $object->removeOption()->insertGetId($data);
        }
        
        return true;
    }
    

    /**
     * 检查短信验证码是否正确
     * @param int $mobile
     * @param str $content
     * @param int $type   1-用户注册，2-忘记密码  , 3-更换账号
     */
    public function verifySms($mobile,$content,$type=1){
    	//验证短信码是否存在，是否过期
    	$map['phone'] =  $mobile;
    	$map['type'] = $type;
    	$result = db('sms_code')->where($map)->find();
    	
    	$rtn['code']=0;
    	if(empty($result)){    		
    		$rtn['msg']='该手机号未发送验证码';
    		return $rtn;
    	} 

    	if($content!=$result['content']){
    		$rtn['msg']='短信验证码不正确';
    		return $rtn;
    	}
    	
    	//15分钟内容有效 
    	if(NOW_TIME>$result['time']+900){
    		$rtn['msg']='短信验证码已过期,请重新发送';
    		return $rtn;
    	}
    	
    	$rtn['code']=1;
    	return $rtn;
    }
    
    
    
}